<?php
/**
 *
 * @Filename index.php $
 *
 * @Author nikm $
 *
 * @Date 2011-12-19 $
 */

//error_reporting(E_ERROR);
error_reporting(E_ALL|E_NOTICE);
ini_set('display_errors', 'off');

define('FROM_INDEX', true);
define('VERSION', 1);
define('ROOT_PATH', dirname(__FILE__).'/');

require ROOT_PATH.'include/lib/load_setting.php';
//load_setting('config_global');

require ROOT_PATH.'mobiapi/conf/config_servlet.php';
require ROOT_PATH.'include/function/global.func.php';
require_once ROOT_PATH.'include/cache/class_cache_factory.php';
require ROOT_PATH.'mobiapi/include/function/servlet.func.php';
//require_once ROOT_PATH.'include/lib/cache.func.php';

// Available modules
$mods = array('user'=>1, 'story'=>1, 'tag'=>1, 'feed'=>1);

//echo "1--- ".microtime()." ---\r\n";

$sid = isset($_SERVER['HTTP_USER_TOKEN']) ? $_SERVER['HTTP_USER_TOKEN'] : '';
$app_key = isset($_SERVER['HTTP_APP_KEY']) ? $_SERVER['HTTP_APP_KEY'] : '';
$app_token = isset($_SERVER['HTTP_APP_TOKEN']) ? $_SERVER['HTTP_APP_TOKEN'] : '';

//if (!isset($sec_auth[$app_key]) || $sec_auth[$app_key] != $app_token) show_msg('ACCESS_DENIED', STATUS_D);

$version = isset($_GET['v']) ? $_GET['v'] : (isset($_POST['v']) ? $_POST['v'] : 1);
$mod = isset($_GET['m']) ? $_GET['m'] : (isset($_POST['m']) ? $_POST['m'] : '');
$act = isset($_GET['a']) ? $_GET['a'] : (isset($_POST['a']) ? $_POST['a'] : '');
$request = isset($_GET['request']) ? $_GET['request'] : (isset($_POST['request']) ? $_POST['request'] : '');

if (API_VERSION != $version) show_msg('VERSION_NOT_MATCH', STATUS_E);

$mod_file = ROOT_PATH.'mobiapi/modules/'.$mod.'.mod.php';
// Module does not exists
if(!file_exists($mod_file) || !isset($mods[$mod]) || !$mods[$mod])
{
	show_msg('ACTION_NOT_EXISTS', STATUS_E);
}

if($request)
{
	$request = parse_json($request);
	
	// Request not in json format
	if(!$request || !is_array($request))
	{
		show_msg('BAD_REQUEST', STATUS_E);
	}
}

$request['sid'] = $sid;
$request['mod'] = $mod;
$request['act'] = $act;

$request = array_merge($request, $_REQUEST);

@header('Content-Type: text/html; charset=utf-8');
//@header('User-Token: '.$sid, false);
//@header('App-Key: '.$app_key, false);
//@header('App-Token:'.$app_token, false);

//echo "2--- ".microtime()." ---\r\n";

require_once $mod_file;
new ModuleObj($request);